<template>
    <div class="goods">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/menu/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/menu/">商品管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <!-- 搜索 -->
            <div class="search">
                <el-input v-model="kw" placeholder="请输入内容" clearable @clear="clearsearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
                <!-- 添加 -->
                <el-button type="info" @click="$router.push('/goods/add')">添加</el-button>
            </div>
            <!-- 表格 -->
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" prop="index" label="#">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="600px">
                </el-table-column>
                <el-table-column prop="goods_price" label="商品价格">
                </el-table-column>
                <el-table-column prop="goods_number" label="商品数量">
                </el-table-column>
                <el-table-column prop="upd_time" label="创建时间">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" @click="update(scope.row)" size="mini">
                        </el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="del(scope.row)" size="mini">
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[3,5,7,10]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="this.total">
            </el-pagination>
        </el-card>
        
        <!-- 编辑的模态框 -->
        <el-dialog title="编辑" :visible.sync="dialogFormVisible1">
            <el-form :rules="rules" ref="form" :model="form" label-width="80px">
                <el-form-item label="商品名称" prop="goods_name">
                    <el-input v-model="form.goods_name"  autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" prop="goods_price">
                    <el-input v-model="form.goods_price" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="商品数量" prop="goods_number">
                    <el-input v-model="form.goods_number" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="商品重量" prop="goods_weight">
                    <el-input v-model="form.goods_weight" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="do_update()">修 改</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
export default {
    data() {
        return {
            kw: '',
            dialogFormVisible1: false,
            tableData: [],
            rolelist: [],
            user: '',
            role: '',
            form: {
                goods_name: '',
                goods_price: '',
                goods_number: '',
                goods_weight:'',
                goods_introduce:'',
                pics:'',
                attrs:'',
            },
            rules: {
                goods_name: [
                    { required: true, message: '商品名称不能为空', trigger: 'blur' },
                ],
                goods_price: [
                    { required: true, message: '商品价格不能为空', trigger: 'blur' },
                ],
                goods_number: [
                    { required: true, message: '商品数量不能为空', trigger: 'blur' },
                ],
                goods_weight: [
                    { required: true, message: '商品重量不能为空', trigger: 'blur' },
                ],
            },
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            total: 0,

            id: 0,
            uid: 0,

        }
    },
    mounted() {
        this.getlist()
    },
    methods: {
        // 请求数据
        async getlist() {
            var res = await this.axios({
                url: '/v1/goods',
                params: this.queryInfo
            })
            console.log(res);
            this.tableData = res.data.data.goods
            this.total = res.data.data.total
            this.tableData.forEach(item=>{
               let smin = new Date(item.upd_time*1000)
                let y = smin.getFullYear()
                var obj ={
                    m:smin.getMonth()+1,
                    d:smin.getDate(),
                    h:smin.getHours(),
                    min:smin.getMinutes(),
                    s:smin.getSeconds(),
                }
                for(let i in obj){
                    obj[i] = obj[i]<10? "0"+obj[i] : obj[i]
                }
             item.upd_time = `${y}-${obj.m}-${obj.d} ${obj.h}:${obj.min}:${obj.s}`
            })
        },
        // 分页
        handleSizeChange(val) {
            this.queryInfo.pagesize = val
            console.log(this.queryInfo.pagesize);
            this.getlist()
        },
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val
            console.log(this.queryInfo.pagenum);
            this.getlist()
        },
        // 删除
        del(row) {

            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                var id = row.goods_id
                console.log(id);
                var res = await this.axios({
                    url: `/v1/goods/${id}`,
                    method: 'DELETE',
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getlist()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        
        // 搜索
        async search() {
        var res = await this.axios({
        url: '/v1/goods',
        params: {
            pagesize: this.queryInfo.pagesize,
            pagenum: this.queryInfo.pagenum,
            query: this.kw
        }
    })
    console.log(res);
    this.tableData = res.data.data.goods
    this.total = res.data.data.total
},
// 清空搜索框
clearsearch() {
    this.queryInfo.query = ''
    this.getlist()
},
// 编辑
update(row) {
    console.log(row,'update');
    this.id = row.goods_id
    this.dialogFormVisible1 = true,
        this.form = row
},
do_update() {
    var id = this.id
    this.$refs.form.validate(async (valid) => {
        if (!valid) {
            return this.$message.error("校验失败")
        } else {
            var res = await this.axios({
                url: `/v1/goods/${id}`,
                method: "PUT",
                data: this.form
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.dialogFormVisible1 = false
                this.$message.success('编辑成功')
                this.dialogFormVisible1 =false
                this.getlist()
            }
        }
    })
},
    }
}
</script>
<style >
.box-card {
    width: 100%;
    margin-top: .9375rem;
}

.search {
    display: flex;

}

.search .el-input__inner {
    width: 300px;
}

.search .el-input {
    width: 300px;
}
</style>